package sybel.pan;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import sybel.DOMPanelInterface;


public class Flow extends javax.swing.JPanel implements DOMPanelInterface{

    /** Creates new form Flow */
    public Flow(Node flow){
        Flow  = (Element)flow;
        initComponents();
        // se il nodo è già stato riempito viene chiamato il metodo che inizializza i campi dei pannelli
        if(Flow.hasChildNodes())
            if(Flow.getLastChild().getNodeName() == "success" || Flow.getLastChild().getNodeName() == "failure"
                || Flow.getLastChild().getNodeName() == "goto")
                InitPanel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextID = new javax.swing.JTextField();
        jCBMainFlow = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        jCBTermination = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextDescription = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jTextGoTo = new javax.swing.JTextField();

        jLabel1.setText("*Name:");

        jTextName.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 0, 0), 2));

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("ID:");

        jCBMainFlow.setText("Main Flow");

        jLabel3.setText("Termination");

        jCBTermination.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Success", "Failure", "Go To" }));
        jCBTermination.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCBTerminationItemStateChanged(evt);
            }
        });

        jLabel4.setText("Description:");

        jTextDescription.setColumns(20);
        jTextDescription.setRows(5);
        jScrollPane1.setViewportView(jTextDescription);
        jTextDescription.setLineWrap(true);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 243, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 83, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextName, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextID, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jCBMainFlow, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCBTermination, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTextGoTo, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 358, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(117, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jTextID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCBMainFlow, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jCBTermination, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextGoTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jTextGoTo.setVisible(false);
    }// </editor-fold>//GEN-END:initComponents

    private void jCBTerminationItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCBTerminationItemStateChanged
        if(jCBTermination.getSelectedIndex() == 2)
            jTextGoTo.setVisible(true);
        else
            jTextGoTo.setVisible(false);
        jTextGoTo.repaint();
        jTextGoTo.validate();
    }//GEN-LAST:event_jCBTerminationItemStateChanged

    public void InitPanel(){
        setFlowName(Flow.getAttribute("name"));
        setID(Flow.getAttribute("id"));
        if(Flow.getFirstChild().getNodeName() == "description")
            setDescription(Flow.getElementsByTagName("description").item(0).getTextContent());

        if(Flow.hasAttribute("main"))
            setMainFlow(true);


        setTermination(Flow.getLastChild().getNodeName());
    }


    public Node saveData(){
        Document doc = Flow.getOwnerDocument();

        if(!getFlowName().isEmpty())
            Flow.setAttribute("name",getFlowName());
        if(!getID().isEmpty())
            Flow.setAttribute("id",getID());

        if(!getDescription().isEmpty()){
            Element Description = doc.createElement("description");
            Description.setTextContent(getDescription());
            // si inserisce il nodo description come primo figlio di flow, se esiste già lo si sostituisce
            if(Flow.getFirstChild() == null)
                Flow.appendChild(Description);
            else if(Flow.getFirstChild().getNodeName() == "description")
                Flow.replaceChild(Description, Flow.getFirstChild());
            else
                Flow.insertBefore(Description, Flow.getFirstChild());
        }

        if(isMainFlow()){
            Flow.setAttribute("main","true");
        }

        int temp = jCBTermination.getSelectedIndex();
        Element termination = null;
        switch(temp){
            case 0:
                termination = doc.createElement("success");
                break;
            case 1:
                termination = doc.createElement("failure");
                break;
            case 2:
                termination = doc.createElement("goto");
                termination.setTextContent(jTextGoTo.getText());
                break;
        }
        
        if(Flow.hasChildNodes() && (Flow.getLastChild().getNodeName() == "success" || Flow.getLastChild().getNodeName() == "failure"
                || Flow.getLastChild().getNodeName() == "goto"))
            Flow.replaceChild(termination, Flow.getLastChild());
        else
            Flow.appendChild(termination);

        return (Node)Flow;
    }


    public String getID(){
        return jTextID.getText();
    }

    public String getFlowName(){
        return jTextName.getText();
    }

    public String getDescription(){
        return jTextDescription.getText();
    }

    public String getTermination(){
        int temp = jCBTermination.getSelectedIndex();
        switch(temp){
            case 0:
                return "success";
            case 1:
                return "failure";
            case 2:
                return "go_to";
            default:
                return null;
        }
    }

    public boolean isMainFlow(){
        return jCBMainFlow.isSelected();
    }

    public void setID(String s){
        jTextID.setText(s);
    }

    public void setFlowName(String s){
        jTextName.setText(s);
    }

    public void setDescription(String s){
        jTextDescription.setText(s);
    }

    public void setTermination(String s){
        if(s == "success")
            jCBTermination.setSelectedIndex(0);
        else if(s == "failure")
            jCBTermination.setSelectedIndex(1);
        else{
            jCBTermination.setSelectedIndex(2);
            jTextGoTo.setText(Flow.getLastChild().getTextContent());
        }
    }

    public void setMainFlow(boolean b){
        if(b)
            jCBMainFlow.setSelected(b);
        else
            jCBMainFlow.setSelected(false);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCBMainFlow;
    private javax.swing.JComboBox jCBTermination;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextDescription;
    private javax.swing.JTextField jTextGoTo;
    private javax.swing.JTextField jTextID;
    private javax.swing.JTextField jTextName;
    // End of variables declaration//GEN-END:variables
    private Element Flow;

    public void initPanel() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
